/**
 * 
 */
package ac.svn.accesscontrol.ui.window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import ac.svn.accesscontrol.store.IResources;
import ac.svn.accesscontrol.ui.JMenu;
import ac.svn.accesscontrol.ui.panel.UserBasicInfoEditorPanelI;
import ac.svn.accesscontrol.utils.UIUtility;
import ac.svn.accesscontrol.utils.Utility;

/**
 * @author Enigmanic
 *
 */
public class UserInformationEditorWindow extends JFrame 
{

	public static final String E_TAB_ACCESS_RIGHTS 	= "TAB_ACCESS_EDITOR-";

	public static final String TAB_U_BASIC_INFO 	= "Basic Information";
	public static final String TAB_U_GROUP_MEMBERSHIP 	= "Group Membership";
	public static final String TAB_U_ACCESS_RIGHTS	= "Access Rights";

	private String editorId = null;
	/**
	 * 
	 */
	private static final long serialVersionUID 		= 1L;

	private UserBasicInfoEditorPanelI 	basicInfoEditorPanel;
	private JButton 			btnFinish;

	private Map<Component,String> 	tabCompMap;
	private JPanel 			basePanel;
	private JButton 			addUserButton;
	private JButton 			deleteUserButton;
	private JMenuBar 			menuBar;
	private JMenu 			actionMenu;
	private JMenuItem 			removeUserMI;
	private JMenuItem 			addUserMI;
	private JMenuItem 			editUserMI;
	private JMenuItem 			closeMI;
	private JMenu 			mnHelp;
	private JMenuItem 			helpTopicsMI;
	private JButton 			editUserButton;
	private JList 			userListView;
	private JToolBar			statusBar;
	private JPanel			statusPanel;

	private Component centerComponent;

	public UserInformationEditorWindow()
	{
		super();
		setTitle("User Information Editor");
		setBackground(Color.WHITE);
		setSize(800,501);

		Border border = UIUtility.setAllShadowedBorder(null);
		Color WHITE = Color.WHITE;

		getContentPane().setLayout(new BorderLayout(0, 0));

		tabCompMap = new HashMap<Component,String>();

		basePanel = new JPanel();
		getContentPane().add(basePanel, BorderLayout.CENTER);
		basePanel.setLayout(new BorderLayout(0, 0));
		basePanel.setBorder(border);

		statusBar = new JToolBar();
		statusBar.setBackground(Color.WHITE);
		statusBar.setFloatable(false);
		UIUtility.setAllShadowedBorder(statusBar);

		statusPanel = new JPanel((new FlowLayout(FlowLayout.LEFT)), true);
		statusPanel.setBackground(Color.WHITE);

		JPanel pnlButton = new JPanel();
		pnlButton.setBackground(Color.WHITE);

		basePanel.add(statusBar, BorderLayout.SOUTH);
		basePanel.setBackground(Color.WHITE);

		statusBar.setBorder(UIUtility.setAllShadowedBorder(null));
		pnlButton.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));

		btnFinish = new JButton("Finish");
		btnFinish.setMnemonic('F');
		pnlButton.add(btnFinish);

		statusBar.add(statusPanel);
		statusBar.add(pnlButton);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBackground(Color.WHITE);

		scrollPane.setBorder(new TitledBorder(UIUtility.setAllShadowedBorder(null), "Users Listing", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		scrollPane.setPreferredSize(new Dimension(200, 30));
		basePanel.add(scrollPane, BorderLayout.WEST);

		userListView = new JList();
		userListView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		scrollPane.setViewportView(userListView);

		JLabel userListTitleLabel = new JLabel("Current Users");
		userListTitleLabel.setOpaque(true);	
		userListTitleLabel.setBackground(Color.WHITE);

		UIUtility.setAllShadowedBorder(userListTitleLabel);
		scrollPane.setColumnHeaderView(userListTitleLabel);

		basicInfoEditorPanel = new UserBasicInfoEditorPanelI();
		basePanel.add(basicInfoEditorPanel, BorderLayout.CENTER);

		tabCompMap.put(basicInfoEditorPanel,TAB_U_BASIC_INFO);

		JToolBar toolBar = new JToolBar(JToolBar.VERTICAL);
		toolBar.setBackground(Color.WHITE);
		//headerPanel.add(toolBar, BorderLayout.SOUTH);
		getContentPane().add(toolBar, BorderLayout.WEST);
		addUserButton    = new JButton(Utility.getImageIcon(IResources.LOC_ICN_ADD_USER_SMALL));
		//addUserButton.setIcon(new CompoundIcon(Axis.Y_AXIS, 2,(Utility.getImageIcon(IResources.LOC_ICN_ADD_USER_SMALL)), (new TextIcon(addUserButton,"Add"))));
		addUserButton.setBackground(WHITE);
		addUserButton.setToolTipText("Add User");

		deleteUserButton    = new JButton(Utility.getImageIcon(IResources.LOC_ICN_DELETE_USER_SMALL));
		//deleteUserButton.setIcon(new CompoundIcon(Axis.Y_AXIS, 2,(Utility.getImageIcon(IResources.LOC_ICN_DELETE_USER_SMALL)), (new TextIcon(deleteUserButton,"Delete"))));
		deleteUserButton.setToolTipText("Delete User");
		deleteUserButton.setBackground(WHITE);

		editUserButton    = new JButton(Utility.getImageIcon(IResources.LOC_ICN_EDIT_USER_SMALL));
		//editUserButton.setIcon(new CompoundIcon(Axis.Y_AXIS, 2,(Utility.getImageIcon(IResources.LOC_ICN_EDIT_USER_SMALL)), (new TextIcon(editUserButton,"Edit"))));
		editUserButton.setToolTipText("Edit User");
		editUserButton.setBackground(WHITE);

		toolBar.add(addUserButton);
		toolBar.add(editUserButton);
		toolBar.add(deleteUserButton);

		toolBar.setFloatable(false);
		UIUtility.setAllShadowedBorder(toolBar);

		menuBar = new JMenuBar();
		menuBar.setBackground(Color.WHITE);
		UIUtility.setAllShadowedBorder(menuBar);	

		actionMenu = new JMenu("Action");
		menuBar.add(actionMenu);

		addUserMI = new JMenuItem("Add User");
		actionMenu.add(addUserMI);

		editUserMI = new JMenuItem("Edit User");
		actionMenu.add(editUserMI);

		removeUserMI = new JMenuItem("Remove User");
		actionMenu.add(removeUserMI);

		actionMenu.addSeparator();

		closeMI = new JMenuItem("Close");
		actionMenu.add(closeMI);

		mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);

		helpTopicsMI = new JMenuItem("Help Topics");
		mnHelp.add(helpTopicsMI);

		setJMenuBar(menuBar);

		registerListener();
		exportComponents();
	}


	private void exportComponents() {
	}


	private void registerListener() {

		basicInfoEditorPanel.getTextName().addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				basicInfoEditorPanel.isDataComplete();
			}
		});	
		basicInfoEditorPanel.getTextSVNUserId().addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				basicInfoEditorPanel.isDataComplete();
			}
		});

	}

	public void setBasicInfoComplete(boolean basicInfoComplete) {
	}

	public UserBasicInfoEditorPanelI getBasicInfoEditorPanel() {
		return basicInfoEditorPanel;
	}


	public JButton getBtnFinish() {
		return btnFinish;
	}

	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		UserInformationEditorWindow editor = new UserInformationEditorWindow();
		editor.setDefaultCloseOperation(3);
		editor.getBasicInfoEditorPanel().setData("Ankush","Ankush.chaudhary@igate,com", "Recording Kernel","aChaudhary","User is Configuration Manager!");
		editor.setBasicInfoComplete(editor.getBasicInfoEditorPanel().isDataComplete());
		editor.setVisible(true);
	}

	public void setCenteredLocation()
	{
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((int)(size.getWidth()/2 - getWidth()/2),(int)(size.getHeight()/2-getHeight()/2)); 
	}

	public void setVisible(boolean b)
	{
		setCenteredLocation();
		super.setVisible(b);
	}

	public void resetData()
	{
		basicInfoEditorPanel.resetData();
	}


	protected String getEditorId() {
		if(null == editorId)
		{
			editorId = UUID.randomUUID().toString();
		}
		return editorId;
	}


	protected void setEditorId(String editorId) {
		this.editorId = editorId;
	}


	public JButton getEditUserButton() {
		return editUserButton;
	}


	public void setEditUserButton(JButton editUserButton) {
		this.editUserButton = editUserButton;
	}


	public JButton getAddUserButton() {
		return addUserButton;
	}


	public JButton getDeleteUserButton() {
		return deleteUserButton;
	}


	public JMenuItem getRemoveUserMI() {
		return removeUserMI;
	}


	public JMenuItem getAddUserMI() {
		return addUserMI;
	}


	public JMenuItem getEditUserMI() {
		return editUserMI;
	}


	public JMenuItem getCloseMI() {
		return closeMI;
	}


	public JMenuItem getHelpTopicsMI() {
		return helpTopicsMI;
	}


	public JList getUserListView() {
		return userListView;
	}


	/**
	 * @return the statusBar
	 */
	public JToolBar getStatusBar() {
		return statusBar;
	}


	/**
	 * @return the statusPanel
	 */
	public JPanel getStatusPanel() {
		return statusPanel;
	}


	/**
	 * @return the basePanel
	 */
	public JPanel getBasePanel() {
		return basePanel;
	}


	/**
	 * @param basePanel the basePanel to set
	 */
	public void setBasePanel(JPanel basePanel) {
		this.basePanel = basePanel;
	}

	public void setCenterView(Component componentToAdd,Component componentToRemove)
	{
		if(null == centerComponent)
		{
			centerComponent = componentToRemove;
		}
		
		if(centerComponent.equals(componentToRemove))
		{
			basePanel.remove(componentToRemove);
			basePanel.add(componentToAdd,BorderLayout.CENTER);
			centerComponent = componentToAdd;
			
			SwingUtilities.invokeLater(new Runnable() 
			{
				@Override
				public void run() 
				{
					SwingUtilities.updateComponentTreeUI(basePanel);
				}
			});
			
		}
	}



}
